package com.example.homework.repositories

import com.example.homework.db.daos.WordDao
import com.example.homework.db.tables.Words
import kotlinx.coroutines.flow.Flow

class WordRepository(val wordDao: WordDao) {
    val words: Flow<List<Words>> = wordDao.getLastFive()

    suspend fun addWord(word: String) {
        val anyWord: Words? = wordDao.getWord(word)
        if (anyWord == null) {
            wordDao.insert(Words(word, 1))
        } else {
            anyWord.count++
            wordDao.update(anyWord)
        }
    }

    suspend fun clearAll() {
        wordDao.deleteAll()
    }
}